package com.ChainBizPro.system.controller;

import com.ChainBizPro.system.annotation.AuthCheck;
import com.ChainBizPro.system.common.BaseResponse;
import com.ChainBizPro.system.common.DeleteRequest;
import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.common.ResultUtils;
import com.ChainBizPro.system.constant.UserConstant;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.exception.ThrowUtils;
import com.ChainBizPro.system.model.domain.Stock;
import com.ChainBizPro.system.model.dto.stock.StockAddRequest;
import com.ChainBizPro.system.model.dto.stock.StockQueryRequest;
import com.ChainBizPro.system.model.dto.stock.StockUpdateMyRequest;
import com.ChainBizPro.system.service.StockService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 库存商品接口
 *
 * @author liusijia
 */
@RestController
@RequestMapping("/stok")
@Api(tags = "库存商品接口")
@Slf4j
public class StockController {

    @Resource
    private StockService stockService;

    // region 增删改查

    /**
     * 创建库存商品(仅管理员)
     *
     * @param stockAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建库存商品(仅管理员)")
    public BaseResponse<Integer> addStock(@RequestBody StockAddRequest stockAddRequest, HttpServletRequest request) {
        if (stockAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (stockAddRequest.getProductName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品名称不合法");
        }
        if (stockAddRequest.getQuantity() == null || stockAddRequest.getQuantity() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品数量不合法");
        }
        if (stockAddRequest.getUnitPrice() == null || stockAddRequest.getUnitPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品单价不合法");
        }
        Stock stock = new Stock();
        boolean result = stockService.newStock(stockAddRequest, request);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(stock.getProductId());
    }

    /**
     * 删除库存商品(仅管理员)
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除库存商品(仅管理员)")
    public BaseResponse<Boolean> deleteStock(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        boolean result = stockService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 更新库存商品信息(仅管理员)
     *
     * @param stockUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新库存商品信息(仅管理员)")
    public BaseResponse<Boolean> updateStock(@RequestBody StockUpdateMyRequest stockUpdateRequest, HttpServletRequest request) {
        if (stockUpdateRequest == null || stockUpdateRequest.getProductId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (stockUpdateRequest.getProductName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品名称不合法");
        }
        if (stockUpdateRequest.getQuantity() == null || stockUpdateRequest.getQuantity() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品数量不合法");
        }
        if (stockUpdateRequest.getUnitPrice() == null || stockUpdateRequest.getUnitPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品单价不合法");
        }
        Stock stock = new Stock();
        BeanUtils.copyProperties(stockUpdateRequest, stock);
        boolean result = stockService.updateById(stock);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取所有商品库存
     *
     * @param stockQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取所有商品库存")
    public BaseResponse<Page<Stock>> listStockByPage(@RequestBody StockQueryRequest stockQueryRequest) {
        long current = stockQueryRequest.getCurrent() == 0 ? 1 : stockQueryRequest.getCurrent();
        long size = stockQueryRequest.getPageSize() == 0 ? 10 : stockQueryRequest.getPageSize();
        //分页信息封装Page对象
        Page<Stock> pageParam = new Page(current, size);
        Page<Stock> queryWrapper = stockService.getQueryWrapper(pageParam, stockQueryRequest);
        return ResultUtils.success(queryWrapper);
    }
    // endregion

}
